package top.bugger168.checkcode.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import top.bugger168.checkcode.model.CheckCodeParamsDto;
import top.bugger168.checkcode.model.CheckCodeResultDto;
import top.bugger168.checkcode.service.CheckCodeService;

import javax.annotation.Resource;

/**
 * 验证码服务接口
 *
 * @author wanghaidong
 * @date 2023/08/28
 */
@Api(value = "验证码服务接口")
@RestController
public class CheckCodeController {

    @Resource(name = "PicCheckCodeService")
    private CheckCodeService picCheckCodeService;
    @Resource(name = "NumberLetterCheckCodeGenerator")
    private CheckCodeService.CheckCodeGenerator numberLetterCheckCodeGenerator;
    @Resource(name = "UUIDKeyGenerator")
    private CheckCodeService.KeyGenerator uuidKeyGenerator;
    @Resource(name = "RedisCheckCodeStore")
    private CheckCodeService.CheckCodeStore redisCheckCodeStore;


    @ApiOperation(value="生成验证信息", notes="生成验证信息")
    @PostMapping(value = "/pic")
    public CheckCodeResultDto generatePicCheckCode(CheckCodeParamsDto checkCodeParamsDto){
        checkCodeParamsDto.setCheckCodeType("pic");
        return picCheckCodeService.generate(checkCodeParamsDto);
    }

    @ApiOperation(value="校验", notes="校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "业务名称", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "key", value = "验证key", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType="query")
    })
    @PostMapping(value = "/verify")
    public Boolean verify(String key, String code){
        return picCheckCodeService.verify(key,code);
    }

    /**
     * 通过手机号/邮箱获取验证码,暂存到redis
     * @param param1 手机号
     * @return 验证码
     */
    @PostMapping(value = "/phone")
    public CheckCodeResultDto verify(String param1){
        // 获取验证码的key和code,在redis中存储5min
        CheckCodeParamsDto checkCodeParamsDto = new CheckCodeParamsDto();
        if(param1.contains("@")){
            checkCodeParamsDto.setCheckCodeType("email");
        }else{
            checkCodeParamsDto.setCheckCodeType("sms");
        }
        return picCheckCodeService.generate(checkCodeParamsDto);
    }
}
